from django.db import models

from Group.models import TeamDetail, TeamMemberShip


class MemberTraining(models.Model):
    """
        对于单次的训练，添加单次的定时任务
        对于循环的训练，添加循环的定时任务
    """

    TRAINING_TYPE_CHOICES = [
        (0, 'Regular Training'),
        (1, 'One-time Training')
    ]
    team = models.ForeignKey(TeamDetail, on_delete=models.CASCADE, related_name='team_training')
    member = models.ForeignKey(TeamMemberShip, on_delete=models.CASCADE, related_name='training')

    name = models.CharField(max_length=128, verbose_name='训练名称')
    training_type = models.SmallIntegerField(choices=TRAINING_TYPE_CHOICES, default=1, verbose_name='训练类型')
    start_time = models.DateTimeField(verbose_name='开始时间')
    end_time = models.DateTimeField(verbose_name='结束时间')
    recurrence_days = models.JSONField(blank=True, null=True)  # 对于固定训练，存储每周的训练天数

    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now_add=True, verbose_name='更新时间')
    is_delete = models.BooleanField(default=False, verbose_name='删除标记')

    """
        每个训练计划都会关联一个或多个 celery 任务ID，用户检查用户是否定期完成了训练计划
    """
    tasks_id = models.JSONField(blank=True, null=True, verbose_name='Celery Task IDs')

    def __str__(self):
        return f'{self.team.team.name} - {self.get_training_type_display()}'

    class Meta:
        ordering = ['-create_time']


class MemberTrainingRecord(models.Model):
    """
        每当用户签到或者请假的时候，会自动生成一条记录
    """

    member = models.ForeignKey(TeamMemberShip, on_delete=models.CASCADE, related_name='training_record')
    training = models.ForeignKey(MemberTraining, on_delete=models.CASCADE, related_name='training_record')

    RECORD_TYPE_CHOICES = [
        (0, '缺勤'),
        (1, '已签到'),
        (2, '请假'),
    ]
    record_type = models.SmallIntegerField(choices=RECORD_TYPE_CHOICES, default=0, verbose_name='签到状态')

    # 备注
    remark = models.CharField(max_length=128, blank=True, null=True, verbose_name='备注')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    def __str__(self):
        return self.training.name + ' - ' + self.RECORD_TYPE_CHOICES[self.record_type][1]
